<?php
/*
 * 王涛，更新程序
 */

require 'include.php';
require 'config.php';

#初始化数据库
$link = wt_SQL::connect(HOST,USER,PASSW,DB);


for (;;)
{
	$sql = 'SELECT `Blog_Feed` FROM blog_info WHERE `Blog_State` =1';

	#得到源列表
	$blogRss = get_one_arr($sql);
	foreach ($blogRss as $feed)
	{
		#获得所有地址列表
		$aLink[$feed] = selectPostLink($feed);
		$post = wt_xml::readRSS($feed);
		addPost($post['post'], $aLink);
		sleep(520);
	}
	//addPost();
	//print_r($post);
}

//print_r($aLink['http://www.cnbeta.com/backend.php']);
//print_r($blogRss);
//print_r($rssPost['post']);

#加入文章
function addPost($post, $aLink)
{
	try {
		$phpdb = new PDO('mysql:host=' . HOST .';dbname=' . DB, USER, PASSW);
	}
	catch (PDOException $e)
	{
		die ('Error!: ' . $e->getMessage() . '<br/>');
	}
	
	foreach ($post as $p)
	{		
		//print_r($p);
		if(in_array($p['link'], $aLink[$p['feed']]))
		{
			//echo $aLink[$p['feed']] . "\n";
			//$strLink = $p['link'] . ' => exist. Time:' . date('Y-m-d-H-i-s') . "\n";
			//writeLog($strLink);
			return false;
			//exit();
		}
		else
		{
			$postCreator = empty($p['dc_creator']) ? empty($p['author']) ? wt_xml::getDomain($p['feed']) : $p['author'] : $p['dc_creator'];
			$postContent = empty($p['content_encoded']) ? $p['description'] : $p['content_encoded'];
			$slashComments = empty($p['slash_comments']) ? 0 : $p['slash_comments'];
			$sql = 'INSERT INTO `feed`.`post_into` (`Pid` ,`Post_Title` ,`Post_Link` ,`Post_PubDate` ,`Post_Creator` ,`Post_Content` ,`Post_Comments` ,`Post_State` ,`Post_Feed`) VALUES (NULL ,\'' . addslashes($p['title']) . '\',\'' . addslashes($p['link']) . '\',\'' . $p['pubDate'] . '\',\'' . addslashes($postCreator) . '\',\'' . addslashes($postContent) . '\',\'' . $slashComments . '\',' . '1' . ',\'' . addslashes($p['feed']) . '\')';
			//echo $sql . "\n";
			try {
				$phpdb -> exec($sql);
				$phpdb -> commit();
				$strPosr = $p['title'] . ' => is OK. | link => ' . $p['link'] . ' | Time:' . date('Y-m-d H:i:s') . "\n";
				Log::writeLog($strPosr);
			}
			catch (Exception $e) {
				//$phpdb -> rollBack();
				$strPosr = $p['title'] . ' => is OK. | link => ' . $p['link'] . ' | Time:' . date('Y-m-d H:i:s') . "\n";
				Log::writeLog($strPosr);
				Log::writeLog("Failed: " . $e->getMessage() . ' | Time:' . date('Y-m-d H:i:s') . "\n");
			}
		}
	//echo '更新成功';
	}
}


/*
 * 写入日志函数
 */
function writeLog($str)
{
	$file = dirname(__FILE__) . '/Log/' . date('Y-m-d') . '.txt';
	if (file_exists($file))
	{
		if (is_writable($file))
		{
			if (!$handle = fopen($file, 'a'))
			{
				echo '不能打开文件' . $file;
				return false;
			}
			if (fwrite($handle, $str) === false)
			{
				echo '不能写入到文件' . $file;
				return false;
			}
			fclose($handle);
		}
		else
		{
		
			echo '说好的文件：' . $file;
		}
	}
	else
	{
		fopen($file, "w+");
	}
}

/*
 * 得到文章地址列表
 */
function selectPostLink($feed)
{
	$postLinkSQL = 'SELECT  `Post_Link` FROM  `post_into` WHERE  `Post_Feed` =  \'' . $feed . '\'';
	return get_one_arr($postLinkSQL);
}


/*
 * 返回数组
 *
 */

function get_one_arr($sql)
{
	//require 'config.php';
	#初始化数据库
	try {
		$phpdb = new PDO('mysql:host=127.0.0.1;dbname=feed', 'root', 'tto123');
	}
	catch (PDOException $e)
	{
		die ('Error!: ' . $e->getMessage() . '<br/>');
	}
	
	$sth = $phpdb -> prepare($sql);
	$sth -> execute();
	//返回一列
	return $result = $sth -> fetchAll(PDO::FETCH_COLUMN, 0);
}